package com.kaiyidiannao.activity.user_detail;

import android.content.Intent;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import com.kaiyidiannao.common.BaseActionBarActivity;
import com.kaiyidiannao.data.DataCenter;
import com.kaiyidiannao.main.R;
import com.kaiyidiannao.model.bean.UserBean;
import com.kaiyidiannao.model.req.model.GetUserDetail;
import com.kaiyidiannao.view.ActionBarView;
import com.taplinker.core.util.LogUtil;

import java.util.ArrayList;
import java.util.Observable;
import java.util.Observer;

/**
 * Created by wzg on 14-4-15.
 */
public class UserDetail extends BaseActionBarActivity implements Observer {
    private ImageView user_detail_edit;
    private TextView user_detail_username;
    private TextView user_detail_truename;
    private TextView user_detail_tel;
    private TextView user_detail_address;
    private TextView user_detail_qq;

    private ArrayList<UserBean> userBean_id = DataCenter.userLogin;
    private ArrayList<UserBean> userBeans = DataCenter.userBeans;
    UserBean userBean;
    String user_id = userBean_id.get(0).getUserid();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.user_detail);

        // 添加观察者
        DataCenter.getInstance().addObserver(this);

        initID();
        init();

        execute(user_id);
        showProgressDialog();
    }

    @Override
    protected void onRestart() {
        execute(user_id);
        super.onRestart();
    }

    public void execute(String userid){

        new GetUserDetail(userid).execute();
    }

    private void initID() {
        user_detail_username = (TextView) findViewById(R.id.user_detail_username);
        user_detail_truename = (TextView) findViewById(R.id.user_detail_truename);
        user_detail_tel = (TextView) findViewById(R.id.user_detail_tel);
        user_detail_address = (TextView) findViewById(R.id.user_detail_address);
        user_detail_qq = (TextView) findViewById(R.id.user_detail_qq);

        if(userBeans.size() == 0){

        }else {
            user_detail_username.setText(userBeans.get(0).getUsername());
            user_detail_truename.setText(userBeans.get(0).getTruename());
            user_detail_tel.setText(userBeans.get(0).getMobile());
            user_detail_address.setText(userBeans.get(0).getAddress());
            user_detail_qq.setText(userBeans.get(0).getQq());
        }
    }

    public void init() {
        findActionBar();

        user_detail_edit = (ImageView) findViewById(R.id.user_detail_edit);
        user_detail_edit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent mIntent = new Intent(UserDetail.this, EditUserDetail.class);
//                Bundle bundle = new Bundle();
//                bundle.putSerializable("userbean",userBean);
//                mIntent.putExtras(bundle);
                startActivity(mIntent);
            }
        });
    }

    private void findActionBar() {
        actionBarView = (ActionBarView) findViewById(R.id.user_detail_actionBar);
        actionBarView.setTitle("用户详情");
        actionBarView.checkBox.setVisibility(View.GONE);
        actionBarView.button_right_icon2.setVisibility(View.VISIBLE);
        actionBarView.button_right_icon2.setImageBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.user_center_edit_password));
//      修改密码的事件
        actionBarView.button_right_icon2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(UserDetail.this, EditPassword.class);
                startActivity(intent);
            }
        });
    }

    @Override
    public void update(Observable observable, Object data) {
        DataCenter.flag flag = (DataCenter.flag) data;

        if (flag == DataCenter.flag.GET_USER_DETAIL_SUCCESS
                || flag == DataCenter.flag.GET_USER_DETAIL_FAILED) { //类表数据

            String code = DataCenter.code;
            if (flag == DataCenter.flag.GET_USER_DETAIL_SUCCESS) {
                if (code.equals(DataCenter.successFlag)) {
                    closeProgressDialog();
                    userBean = DataCenter.userBeans.get(0);
                    user_detail_username.setText(userBean.getUsername());
                    user_detail_truename.setText(userBean.getTruename());
                    user_detail_address.setText(userBean.getAddress());
                    user_detail_qq.setText(userBean.getQq());
                    user_detail_tel.setText(userBean.getMobile());
                   // Toast.makeText(UserDetail.this, DataCenter.msg, Toast.LENGTH_SHORT).show();
                } else {
                    closeProgressDialog();
                   // Toast.makeText(UserDetail.this, DataCenter.msg, Toast.LENGTH_SHORT).show();
                    return;
                }
            }

            if (flag == DataCenter.flag.GET_USER_DETAIL_FAILED) {
                closeProgressDialog();
                //oast.makeText(UserDetail.this, "failed", Toast.LENGTH_SHORT).show();
            }
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        LogUtil.d(DataCenter.LOG_INFO, UserDetail.class.getSimpleName() + ".onDestroy");
        //删除观察者
        DataCenter.getInstance().deleteObserver(this);
    }


}
